<?php
/**
 * Created by PhpStorm.
 * User: Bzing
 * Date: 2017/7/25
 * Time: 12:34
 */
namespace app\crontab\controller;
use app\models\Models;
use think\Controller;

class Minute extends Controller {
    /**
     * 定时取消未支付订单
     * 每分钟执行计划任务
     * @return string
     */
    public function autoCancelOrder(){
        $order = new Models('order');
        $time = time() - 15 * 60;
        $list = $order->_select(['status'=>Models::STATUS_OFF,'create_time'=>['elt',$time]],'id,uid,type_id,table_id','create_time desc');
        if(empty($list)){
            return 'empty';
        }
        $event_table_ids = [];
        $wine_table_ids  = [];
        $ids = implode(',',array_column($list,'id'));
        // 获取活动发起人id
        $join = new Models('activity_join');
        $uids = $join->getColumn(['status'=>Models::STATUS_ON,'is_originator'=>Models::IS_ORIGINATOR,'uid'=>['in',$ids]],'uid');
        foreach($list as $k=>$v){
            // 获取发起人活动订单的桌号，参加用户订单取消不修改座位状态
            if($v['type_id'] == Models::ORDER_TYPE_EVENT && in_array($v['uid'],$uids)){
                $event_table_ids[] = $v['table_id'];
            }
            // 获取酒水订单的桌号
            if($v['type_id'] == Models::ORDER_TYPE_WINE){
                $wine_table_ids[]  = $v['table_id'];
            }
        }
        // 合并要修改状态的桌号
        $table_ids = implode(',',array_merge($event_table_ids,$wine_table_ids));
        // 更新订单状态
        $bool1 = $order->_save(['id'=>['in',$ids],'status'=>Models::STATUS_OVER]);
        // 更新座位状态
        $table = new Models('table');
        $bool2 = $table->_save(['id'=>['in',$table_ids],'reserve_num'=>['exp','reserve_num-1'],'is_reserve'=>Models::TABLE_NOT]);
        if($bool1 && $bool2){
            return 'success';
        } else {
            return 'error';
        }
    }

    /**
     * 定时开始/取消活动
     * 每个半点执行计划任务
     * @return string
     */
    public function autoCancelEvent(){
        $activity = new Models('activity');
        $time = time();
        $list = $activity->_select(['status'=>Models::STATUS_READY,'begin_time'=>['<',$time],'type_id'=>[['eq',Models::ACTIVITY_TYPE_USER],['eq',Models::ACTIVITY_TYPE_PWD],'or']],'id,join_num,join_min');
        if(empty($list)){
            return 'empty';
        }
        $bool1 = true;
        $bool2 = true;
        $begin_ids  = [];
        $cancel_ids = [];
        foreach($list as $k=>$v){
            // 获取满足开始的活动id
            if($v['join_num'] == $v['join_min']){
                $begin_ids[]  = $v['id'];
            }
            // 获取不满足开始的活动id
            if($v['join_num'] < $v['join_min']){
                $cancel_ids[] = $v['id'];
            }
        }
        // 活动开始
        if($begin_ids){
            $bool1 = $activity->_save(['status'=>Models::STATUS_READY,'begin_time'=>['<',$time],'type_id'=>[['eq',Models::ACTIVITY_TYPE_USER],['eq',Models::ACTIVITY_TYPE_PWD],'or'],'join_num']);
        }
        // 活动取消
        if($cancel_ids){
            $bool2 = $activity->_save(['id'=>['in',$cancel_ids],'status'=>Models::STATUS_CANCEL]);
        }
        if($bool1 && $bool2){
            return 'success';
        } else {
            return 'error';
        }
    }
}